const reactiveMap = new WeakMap();

function deepReactive(obj, handlers = {}) {
    if (typeof obj !== 'object' || obj === null) return obj;
    if (reactiveMap.has(obj)) return reactiveMap.get(obj);
    const proxy = new Proxy(obj, {
        get(target, key, receiver) {
            const value = Reflect.get(target, key, receiver);
            if (typeof handlers.onGet === 'function') {
                handlers.onGet(target, key, value);
            }
            // 递归代理嵌套对象
            return deepReactive(value, handlers);
        },
        set(target, key, value, receiver) {
            const oldValue = target[key];
            const result = Reflect.set(target, key, value, receiver);

            // 如果传入了 onSet 回调，调用它
            if (typeof handlers.onSet === 'function') {
                handlers.onSet(target, key, value, oldValue);
            }

            return result;
        }
    })
    reactiveMap.set(obj, proxy);
    return proxy;
}


const state = deepReactive(data, {
  onGet(target, key, value) {
    console.log('读取属性：', key, '值：', value);
  },
  onSet(target, key, value) {
    console.log('设置属性：', key, '新值：', value);
  }
});